//
//  AyTitleButton.m
//  xWeibo
//
//  Created by alvis mac on 8/4/15.
//  Copyright (c) 2015 alvis. All rights reserved.
//

#import "AyTitleButton.h"
#import "UIImageExtend.h"
#define IWTitleButtonImageW 20

@implementation AyTitleButton

+ (instancetype) titleButton{
    return [[self alloc] init];
}

- (instancetype)initWithFrame:(CGRect)frame{
    self=[super initWithFrame:frame];
    if(self){
        self.adjustsImageWhenHighlighted=NO;
        self.titleLabel.font=[UIFont boldSystemFontOfSize:19];
        self.imageView.contentMode=UIViewContentModeCenter;
        self.titleLabel.textAlignment=NSTextAlignmentRight;
        
        [self setBackgroundImage:[UIImage resizedImageWithName:@"navigationbar_filter_background_highlighted"] forState:UIControlStateHighlighted];
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    return self;
}


- (CGRect)titleRectForContentRect:(CGRect)contentRect{
    CGFloat titleY = 0;
    CGFloat titleX = 0;
    CGFloat titleW = contentRect.size.width - IWTitleButtonImageW;
    CGFloat titleH = contentRect.size.height;
    return CGRectMake(titleX, titleY, titleW, titleH);
}

- (CGRect)imageRectForContentRect:(CGRect)contentRect{
    CGFloat y=0;
    CGFloat w=IWTitleButtonImageW;
    CGFloat x=contentRect.size.width-w;
    CGFloat  h=contentRect.size.height;
    return CGRectMake(x, y, w, h);
}

- (void)setTitle:(NSString *)title forState:(UIControlState)state{
    //CGFloat titleW=[title sizeWithAttributes:<#(UIFont *)#>];
    CGFloat titleW=[title sizeWithFont:self.titleLabel.font].width;
    CGRect frame=self.frame;
    frame.size.width=titleW+IWTitleButtonImageW+5;
    self.frame=frame;
    [super setTitle:title forState:state];
}

@end
